package org.csu.myblogger.myblogger.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.csu.myblogger.myblogger.commom.CommonResponce;
import org.csu.myblogger.myblogger.commom.JwtTokenUtil;
import org.csu.myblogger.myblogger.service.service.AccountService;
import org.csu.myblogger.myblogger.service.service.ArticleService;
import org.csu.myblogger.myblogger.service.service.CommentService;
import org.csu.myblogger.myblogger.vo.AccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.Mapping;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account/")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    //用户登录
    @PostMapping("singon/{userid}")
    public CommonResponce<String> SigOn(@PathVariable String userid,@RequestParam String password ){
        return  accountService.Userlogin(userid,password);
    }
    @PostMapping("signout/{username}")
    public CommonResponce Signout(@PathVariable String userid,@RequestHeader("Authorization")String token){
        try{
            //验证token持有者
            String tsetusername=jwtTokenUtil.getUsernameFromToken(token);
            if (tsetusername!=userid){
                return CommonResponce.createForError(1,"你无权操作该用户");
            }
            return accountService.Userlogout(userid);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @PostMapping("regist/{username}")
    public CommonResponce Register(@PathVariable String username, @RequestBody AccountVo accountVo){
        return accountService.Register(username,accountVo);
    }
    @PostMapping("UpdateUserInfo/{username}")
    public CommonResponce UpdateUserInfo(@PathVariable String username,@RequestHeader("Authorization")String token,
                                         @RequestBody AccountVo accountVo){
        try{
            //验证token持有者
            String tsetusername=jwtTokenUtil.getUsernameFromToken(token);
            if (tsetusername!=username){
                return CommonResponce.createForError(1,"你无权操作该用户");
            }
            return accountService.UpdateUserInfo(accountVo,username);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @PostMapping("DeleteUser/{username}")
    public CommonResponce DeleteUser(@PathVariable String username,@RequestHeader("Authorization")String token){
        try{
            //验证token持有者
            String tsetusername=jwtTokenUtil.getUsernameFromToken(token);
            if (tsetusername!=username){
                return CommonResponce.createForError(1,"你无权操作该用户");
            }
            return accountService.DeleteUser(username);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
    @GetMapping("getAccountInfo/{username}")
    public CommonResponce getAccountInfo(@PathVariable String username,@RequestHeader("Authorization")String token){
        try{
            String testname=jwtTokenUtil.getUsernameFromToken(token);
            if (!username.equals(testname)){
                return CommonResponce.createForError(1,"你无权操作该用户");
            }
            return accountService.getAccountInfo(username);
        } catch (JsonProcessingException e) {
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"token有问题");
        }
    }
}
